package com.yc.dict.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.base.BaseServiceImpl;
import com.yc.dict.entity.Dict;
import com.yc.dict.entity.DictDetail;
import com.yc.dict.mapper.DictDetailMapper;
import com.yc.dict.service.IDictDetailService;
import com.yc.dict.service.IDictService;
import com.yc.dict.vo.DictDetailVo;
import com.yc.util.EntityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台字典详情表 服务实现类
 * </p>
 *
 * @author bao
 * @since 2021-12-10
 */
@RequiredArgsConstructor
@Service
public class DictDetailServiceImpl extends BaseServiceImpl<DictDetailMapper, DictDetail> implements IDictDetailService {

    private final IDictService dictService;

    @Override
    public boolean create(DictDetail dictDetail) {
        return save(dictDetail);
    }

    @Override
    public boolean delete(List<Long> ids) {
        return removeByIds(ids);
    }

    @Override
    public boolean update(DictDetail dictDetail) {
        return updateById(dictDetail);
    }

    @Override
    public DictDetail queryById(Long id) {
        return getById(id);
    }

    @Override
    public List<DictDetail> queryByDictId(Long dictId) {
        QueryWrapper<DictDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DictDetail::getDictId, dictId);
        return list(wrapper);
    }

    @Override
    public List<DictDetail> selectList() {
        QueryWrapper<DictDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(DictDetail::getId);
        return list(wrapper);
    }

    @Override
    public IPage<DictDetail> selectPage(IPage<DictDetail> page) {
        QueryWrapper<DictDetail> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(DictDetail::getId);
        return page(page, wrapper);
    }

    @Override
    public List<DictDetailVo> queryByDictName(String dictName) {
        Dict dict = dictService.queryByName(dictName);
        if (ObjectUtil.isNull(dict)){
            return null;
        }
        List<DictDetail> list = queryByDictId(dict.getId());
        List<DictDetailVo> dictDetailVoList = EntityUtils.toList(list, DictDetailVo::new);
        dictDetailVoList.forEach(e -> e.setDictType(dict.getType()));
        return dictDetailVoList;
    }
}
